package com.lala.service.impl;

import com.google.gson.Gson;
import com.lala.dao.ClientLoginDao;
import com.lala.dao.ClientMessageDao;
import com.lala.dao.ClientOrderDao;
import com.lala.dao.OrderDetailsDao;
import com.lala.dto.ToDriverOrderDto;
import com.lala.entity.ClientLogin;
import com.lala.entity.ClientMessage;
import com.lala.entity.ClientOrder;
import com.lala.entity.OrderDetails;
import com.lala.service.ClientOrderService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * (ClientOrder)表服务实现类
 *
 * @author lixu
 * @since 2020-03-24 09:38:30
 */
@Service("clientOrderService")
public class ClientOrderServiceImpl implements ClientOrderService {
    @Resource
    private ClientOrderDao clientOrderDao;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    OrderDetailsDao orderDetailsDao;

    @Autowired
    ClientLoginDao loginDao;
    @Autowired
    ClientMessageDao messageDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ClientOrder queryById(Integer id) {
        return this.clientOrderDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<ClientOrder> queryAllByLimit(int offset, int limit) {
        return this.clientOrderDao.queryAllByLimit(offset, limit);
    }

    /**
     * @ClassName ClientOrderServiceImpl
     * @Description : 修改订单
     *
     * @param clientOrder
     * @Return : int
     * @Author : lixu
     * @Date : 2020/3/25 18:42
    */
    @Override
    public int update(ClientOrder clientOrder) {
        return this.clientOrderDao.update(clientOrder);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.clientOrderDao.deleteById(id) > 0;
    }

    /**
     * @ClassName ClientOrderServiceImpl
     * @Description : 根据用户id查询所有订单
     *
     * @param clientId
     * @Return : java.util.List<com.lala.entity.ClientOrder>
     * @Author : lixu
     * @Date : 2020/3/25 17:28
    */
    @Override
    public List<ClientOrder> selectByClientId(Integer clientId) {
        return clientOrderDao.selectByClientId(clientId);
    }

    /**
     * @ClassName ClientOrderServiceImpl
     * @Description : 添加订单
     *
     * @param clientOrder
     * @Return : void
     * @Author : lixu
     * @Date : 2020/3/25 11:53
    */
    @Override
    public int save(ClientOrder clientOrder,int detailsId) {
        String uuid= UUID.randomUUID().toString();
        clientOrder.setOrderNumber(uuid);
        clientOrder.setOrderDetailsId(detailsId);
        clientOrder.setStatus(0);
        Date now = new Date();
        clientOrder.setCreatTime(now);
         clientOrderDao.save(clientOrder);
        return clientOrder.getId();
    }

    /**
     * @author 霍明铭
     * @param clientId
     * @param status
     * @return
     * 根据id和status查询对应订单
     */
    @Override
    public List<ClientOrder> getByClientIdAndStatus(Integer clientId, Integer status) {
        return clientOrderDao.getByClientIdAndStatus(clientId,status);
    }

    @Override
    public double getDetailsPay(Integer id) {
        return clientOrderDao.getDetailsPay(id);
    }

    @Override
    public void send(Integer id) {
        ToDriverOrderDto toDriverOrderDto=new ToDriverOrderDto();
       ClientOrder order= clientOrderDao.getOrder(id);
       OrderDetails orderDetails= orderDetailsDao.queryById(order.getOrderDetailsId());
        ClientLogin login=loginDao.queryById(order.getClientId());
        ClientMessage message=messageDao.getMg(login.getId());
        toDriverOrderDto.setLogin(login);
        toDriverOrderDto.setMessage(message);
        toDriverOrderDto.setOrder(order);
        toDriverOrderDto.setOrderDetails(orderDetails);
        rabbitTemplate.convertAndSend("order_exchange","client.order.push",new Gson().toJson(toDriverOrderDto));
    }

    @Override
    public ClientOrder getOrderByNumber(String orderNumber) {
        return clientOrderDao.getOrderByNumber(orderNumber);
    }

    @Override
    public void updateStatus(Integer id) {
        clientOrderDao.updateStatus(id);
    }

    @Override

    public ToDriverOrderDto getJson(String orderNumber) {
        ToDriverOrderDto toDriverOrderDto = new ToDriverOrderDto();
        ClientOrder order = clientOrderDao.getOrderByNumber(orderNumber);
        OrderDetails orderDetails = orderDetailsDao.queryById(order.getOrderDetailsId());
        ClientLogin login = loginDao.queryById(order.getClientId());
        ClientMessage message = messageDao.getMg(login.getId());
        toDriverOrderDto.setLogin(login);
        toDriverOrderDto.setMessage(message);
        toDriverOrderDto.setOrder(order);
        toDriverOrderDto.setOrderDetails(orderDetails);
        return toDriverOrderDto;
    }
    @Override
    public List<ClientOrder> selectAll() {
        return clientOrderDao.selectAll();

    }
}